
//
//  TitleCell.m
//  cactus
//
//  Created by li hongdan on 12-4-17.
//  Copyright (c) 2012年 ifeng. All rights reserved.
//

#import "TitleCell.h"
#import "CLGloble.h"
#import <CoreText/CoreText.h>


@implementation TitleCellLabel

- (void)setTitle:(NSString *)title{
    self.text=title;
    self.attrString=nil;
    if(_isStrNotNull(self.text)){
        NSMutableAttributedString *mbstring=[[NSMutableAttributedString alloc] initWithString:self.text];
        int length=[self.text length];
        // 字体Arial-BoldMT STHeitiSC-Light
        CTFontRef font=CTFontCreateWithName(CFSTR("HelveticaNeue-Medium"), 20, NULL);
        [mbstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, length)];
        CFRelease(font);
        
        // 字体颜色
        CGColorRef color=RGB(0x56, 0x56, 0x5e).CGColor;
        [mbstring addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)color range:NSMakeRange(0, length)];
        
        // 行间距
        float lightSpacing=3.0f;
        // 段落对其方式
        CTTextAlignment textAlignment = kCTTextAlignmentLeft;
        
        CTParagraphStyleSetting paraStyles[3] = {
            
            {.spec = kCTParagraphStyleSpecifierMinimumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierMaximumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        CTParagraphStyleRef pStyle=CTParagraphStyleCreate(paraStyles, 3);
        
        [mbstring addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)pStyle range:NSMakeRange(0, length)];
        CFRelease(pStyle);
        self.attrString=mbstring;
    }
    [self setNeedsDisplay];
}

+(float)cacult:(NSString *)title{
    if(_isStrNotNull(title)){
        NSMutableAttributedString *mbstring=[[NSMutableAttributedString alloc] initWithString:title];
        int length=[title length];
        // 字体Arial-BoldMT STHeitiSC-Light
        CTFontRef font=CTFontCreateWithName(CFSTR("HelveticaNeue-Medium"), 20, NULL);
        [mbstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, length)];
        CFRelease(font);
        
        // 字体颜色
        CGColorRef color=RGB(0x56, 0x56, 0x5e).CGColor;
        [mbstring addAttribute:(id)kCTForegroundColorAttributeName value:(__bridge id)color range:NSMakeRange(0, length)];
        
        // 行间距
        float lightSpacing=3.0f;
        // 段落对其方式
        CTTextAlignment textAlignment = kCTTextAlignmentLeft;
        
        CTParagraphStyleSetting paraStyles[3] = {
            
            {.spec = kCTParagraphStyleSpecifierMinimumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierMaximumLineSpacing,.valueSize = sizeof(float), .value = &lightSpacing},
            
            {.spec = kCTParagraphStyleSpecifierAlignment,.valueSize = sizeof(CTTextAlignment), .value = &textAlignment}
        };
        
        CTParagraphStyleRef pStyle=CTParagraphStyleCreate(paraStyles, 3);
        
        [mbstring addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)pStyle range:NSMakeRange(0, length)];
        CFRelease(pStyle);
        
        CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)mbstring);
//        CGMutablePathRef path=CGPathCreateMutable();
//        CGPathAddRect(path, NULL, self.bounds);
        //CTFrameRef frame=CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

        CGSize size=CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, length), NULL, CGSizeMake(I_DOC_CONTENT_WIDTH, CGFLOAT_MAX), NULL);
        
        CFRelease(framesetter);
        
        return size.height;

    }
    return 56;
}


- (void)drawRect:(CGRect)rect{
    
    if(self.attrString){
        CTFramesetterRef framesetter=CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self.attrString);
        CGMutablePathRef path=CGPathCreateMutable();
        CGPathAddRect(path, NULL, self.bounds);
        CTFrameRef frame=CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
        CGContextRef context=UIGraphicsGetCurrentContext();
        CGContextSetTextMatrix(context, CGAffineTransformIdentity);
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        
        CTFrameDraw(frame, context);
        CGPathRelease(path);
        CFRelease(framesetter);
        
    }
    
}


@end

@interface TitleCell ()
@property (nonatomic,strong)TitleCellLabel *label;

@end

@implementation TitleCell



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        TitleCellLabel *label1=[[TitleCellLabel alloc] init];
        
        //        label1.backgroundColor=ICurrentTheme.docStyle.docBgCollor;
        label1.backgroundColor = [UIColor clearColor];
        
        
        label1.tag=91;
        CGRect rect=CGRectZero;
        //        if ([[[ISGloble shareInstance] systemVersion] doubleValue] >= 7.0) {
        //            rect = CGRectMake(I_DOC_TEXT_MARGIN_SPACE, 20.0f+20.f, I_DOC_CONTENT_WIDTH, 54.0f);
        //   }else{
        rect = CGRectMake(I_DOC_TEXT_MARGIN_SPACE, 20.0f+5, I_DOC_CONTENT_WIDTH, self.bounds.size.height-25.f);
        label1.autoresizingMask=UIViewAutoresizingFlexibleHeight;
        //  }
        label1.frame=rect;
        [self addSubview:label1];
        self.label=label1;
    }
    return self;
}

-(void)setTitle:(NSString *)title{
    [self.label setTitle:title];
   // self.label.frame=CGRectMake(I_DOC_TEXT_MARGIN_SPACE, 20.0f+5, I_DOC_CONTENT_WIDTH, 56.0f);
}


- (void)setAllBackground:(UIColor *)color{
    self.backgroundColor=color;
    // self.label.backgroundColor=color;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end